package org.sacgtug.android;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

public class MyService extends Service {

	
	// 5 seconds
	private static final int LOCATION_MIN_TIME = 5000;

	// 10 meters
	private static final int LOCATION_MIN_DISTANCE = 0;

	// Location
	private static final String LONG = "LONG";
	private static final String LATI = "LATI";

	private LocationManager locationManager;

	private Location currentLocation;

	private NotificationManager notificationManager;
	
	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		
		// Setting up the notification manager
		notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

		// Setting up the location manager
		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		String provider = locationManager.getBestProvider(getLocationCriteria(), true);
		Log.i("## S ##", "provider = " + provider);
		locationManager.requestLocationUpdates(provider, LOCATION_MIN_TIME, LOCATION_MIN_DISTANCE, getLocationListener());
		currentLocation = locationManager.getLastKnownLocation(provider);
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();

	}
	
	private void sendNotification(String message) {

		Intent notificationIntent = new Intent(this, MyActivity.class);
		notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
		notificationIntent.putExtra("location", message);
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
		long when = System.currentTimeMillis();
		Notification notification = new Notification(R.drawable.icon, "Artifactly", when);
		notification.setLatestEventInfo(this, "Artifactly", message, contentIntent);
		notificationManager.notify(100, notification);
		Log.i("## S ##", "Notification message = " + message);
	}
	
	private LocationListener getLocationListener() {

		return new LocationListener() {

			public void onLocationChanged(Location location) {
				Log.i("## S ##", "LocationListener.onLocationChanged()");
				currentLocation = location;
				sendNotification(getLocationAsJSON(location));
			}

			public void onProviderDisabled(String arg0) {
				Log.i("## S ##", "LocationListener.onProviderDisabled()");

			}

			public void onProviderEnabled(String arg0) {
				Log.i("## S ##", "LocationListener.onProviderEnabled()");

			}

			public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
				Log.i("## S ##", "LocationListener.onStatusChanged() provider = " + arg0 + " status = " + arg1);

			}

		};
	}
	
	private Criteria getLocationCriteria() {

		Criteria criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_FINE);
		criteria.setAltitudeRequired(false);
		criteria.setBearingRequired(false);
		criteria.setCostAllowed(false);
		criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
		criteria.setSpeedRequired(false);

		return criteria;
	}
	
	private String getLocationAsJSON(Location location) {

		if (null == location) {
			return "{\"" + LATI + "\":-, \"" + LONG + "\":-}";
		}

		Map<String, String> locationData = new HashMap<String, String>();
		locationData.put(LONG, String.valueOf(location.getLongitude()));
		locationData.put(LATI, String.valueOf(location.getLatitude()));

		return new JSONObject(locationData).toString();
	}

}
